import { Button, Table, Modal, } from 'antd'
import { useEffect, useState } from 'react'
import { http } from '@/utils'
import { useStore } from '@/store'

const Factorscore = (props) => {
  const configModalOpenOk = () => {
    props.changeConfigModal(false)
  }
  const configModalOpenCancel = () => {
    props.changeConfigModal(false)
  }
  const closeModalOpen = () => {
    props.changeConfigModal(false)
  }
  const { loginStore } = useStore()
  const [factorData, setFactorData] = useState([])
  const [columns, setColumns] = useState([{ title: '周期', dataIndex: 'freq' }])
  useEffect(() => {
    let factorListOpt = null
    const getFactor = async () => {
      const res = await http.post('/alarm/deliversign/getAllScore')
      console.log('因子列表-', res.body)
      factorListOpt = res.body.map(item => ({
        title: item.factorName,
        key: item.id,
        value: item.id,
        dataIndex: item.factorParam,
        factorParam: item.factorParam,
        label: item.factorName,
      }))
      openFactorModalOpen()
    }
    const openFactorModalOpen = () => {
      http.post('/alarm/deliversign/findFactorScoreByFeatureIdAndTime', {
        featureId: props.configRow.featureId,
        datetime: props.configRow.datetime
      }).then(res => {
        const response = JSON.parse(res.body.data.replaceAll('NaN', '-'))
        const list = [
            ...response.map((e, i) => ({
            ...e,
            id: `_${i}`,
            featureName: res.body.featureName,
            featureId: res.body.featureId
          })),
        ]
        console.log('因子列表', factorListOpt)
        console.log('因子打分详情', list)
        setColumns([{ title: '周期', dataIndex: 'freq', width: 50 }, ...factorListOpt])
        setFactorData(list)
      })
    }
    // 获取因子打分详情
    if (props.factorModalOpen) {
      getFactor()
    }
  }, [loginStore, props])
  return (
    <>
      <Modal
        title={`因子打分详情`}
        open={props.factorModalOpen}
        onOk={configModalOpenOk}
        onCancel={configModalOpenCancel}
        width={1000}
        footer={[
          <Button key="submit" type="primary" onClick={closeModalOpen}>
            确认
          </Button>
        ]}
      >
        <Table
          style={{ marginTop: 20 }}
          rowKey="id"
          size='middle'
          columns={columns}
          dataSource={factorData}
          pagination={false}
          bordered
        />
      </Modal>
    </>
  )
}
export default Factorscore
